//
//  PicViewController.m
//  dogGroup
//
//  Created by iOS Two on 14-8-13.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "PicViewController.h"
#import "UIImageView+MJWebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@interface PicViewController ()

@end

@implementation PicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"PicViewController"];
    [self refreshData];
}
-(void)viewDidDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"PicViewController"];
}
-(void)refreshData {
    [self showIndeterminateProgress];
    _likeArray = [[NSMutableArray alloc]init];
#warning 改dogId
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"dogImg"];
    [query whereKey:@"dogId" equalTo:@"53eacc0ce4b0540d07bda86b"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error == nil) {
            for (int i = 0; i < objects.count; i++) {
                AVObject *object = [objects objectAtIndex:i];
                [_likeArray addObject:[object objectForKey:@"like"]];
            }
            [_table reloadData];
            [HUD hide:YES];
        } else {
            //[self alertViewShow:@"网络未连接"];
            [HUD hide:YES];
        }
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"reloadData"object:nil];
    
    if (version >= 7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.text = @"狗狗萌图";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    } else {
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        self.navigationItem.title = @"狗狗萌图";
    }
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight - 64) style:UITableViewStylePlain];
    if (version >= 7) {
        
    } else {
        _table.frame = CGRectMake(0, 0, 320, kHeight - 44);
    }
    _table.delegate = self;
    _table.dataSource = self;
    _table.rowHeight = 152;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIImageView *background = [[UIImageView alloc]initWithFrame:self.view.frame];
    background.image = [UIImage imageNamed:@"img_mainbackground"];
    [_table setBackgroundView:background];
    [self.view addSubview:_table];
    if (![self netWorkCheck]) {
        [self alertViewShow:@"网络未连接"];
    } else {
        [self getData];
    }
}

-(void)getData {
    [self showIndeterminateProgress];
    _imageViewArray = [[NSMutableArray alloc]init];
    _imgIdArray = [[NSMutableArray alloc]init];
    _likeArray = [[NSMutableArray alloc]init];
    _imgArray = [[NSMutableArray alloc]init];
#warning 改dogId
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"dogImg"];
    [query whereKey:@"dogId" equalTo:@"53eacc0ce4b0540d07bda86b"];
    [query whereKey:@"success" equalTo:[NSNumber numberWithBool:YES]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i < objects.count; i++) {
            AVObject *object = [objects objectAtIndex:i];
            [_imgIdArray addObject:object.objectId];
            [_imgArray addObject:[object objectForKey:@"imgName"]];
            [_likeArray addObject:[object objectForKey:@"like"]];
            
            UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(3+(i%2)*160, 3, 155, 146)];
            leftImage.clipsToBounds = YES;
            leftImage.contentMode = UIViewContentModeScaleAspectFill;
            leftImage.userInteractionEnabled = YES;
#warning 改端口
            [leftImage setImageURLStr:[NSString stringWithFormat:@"http://star-app-pic.b0.upaiyun.com/dog/samoyed/%@!StarCommonPicSmall",[object objectForKey:@"imgName"]] placeholder:[UIImage imageNamed:@"img_loading"]];
            leftImage.tag = i;
            [leftImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
            [_imageViewArray addObject:leftImage];
        }
        [_table reloadData];
        [HUD hide:YES];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _imageViewArray.count / 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    if (indexPath.row < _imageViewArray.count && indexPath.row < _likeArray.count) {
        UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 152)];
        back.image = [UIImage imageNamed:@"img_picback"];
        [cell.contentView addSubview:back];
        
        [cell.contentView addSubview:[_imageViewArray objectAtIndex:indexPath.row * 2]];
        [cell.contentView addSubview:[_imageViewArray objectAtIndex:indexPath.row * 2 + 1]];

        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(3, 125, 155, 24)];
        leftView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.55];
        leftView.userInteractionEnabled = NO;
        [cell.contentView addSubview:leftView];
        
        UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(162, 125, 155, 24)];
        rightView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.55];
        rightView.userInteractionEnabled = NO;
        [cell.contentView addSubview:rightView];
        
        UILabel *leftLike = [[UILabel alloc]initWithFrame:CGRectMake(31, 131, 100, 12)];
        leftLike.font = [UIFont systemFontOfSize:14];
        leftLike.textColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        leftLike.backgroundColor = [UIColor clearColor];
        leftLike.text = [NSString stringWithFormat:@"%@",[_likeArray objectAtIndex:indexPath.row * 2]];
        [cell.contentView addSubview:leftLike];
        
        UILabel *rightLike = [[UILabel alloc]initWithFrame:CGRectMake(191, 131, 100, 12)];
        rightLike.font = [UIFont systemFontOfSize:14];
        rightLike.textColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        rightLike.backgroundColor = [UIColor clearColor];
        rightLike.text = [NSString stringWithFormat:@"%@",[_likeArray objectAtIndex:indexPath.row * 2 + 1]];
        [cell.contentView addSubview:rightLike];
        
        UIImageView *leftHeart = [[UIImageView alloc]initWithFrame:CGRectMake(12, 131, 12, 12)];
        leftHeart.image = [UIImage imageNamed:@"img_heart"];
        [cell.contentView addSubview:leftHeart];
        
        UIImageView *rightHeart = [[UIImageView alloc]initWithFrame:CGRectMake(172, 131, 12, 12)];
        rightHeart.image = [UIImage imageNamed:@"img_heart"];
        [cell.contentView addSubview:rightHeart];
    }
    return cell;
}

-(void)buttonClick:(UITapGestureRecognizer *)sender {
    [MobClick event:@"PicVCLookBigImg"];
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:_imgArray.count];
    for (int i = 0; i < _imageViewArray.count; i++) {
        NSString *string = [NSString stringWithFormat:@"http://star-app-pic.b0.upaiyun.com/dog/samoyed/%@",[_imgArray objectAtIndex:i]];
            // 替换为中等尺寸图片
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:string]; // 图片路径
        photo.srcImageView = _imageViewArray[i]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = sender.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    browser.imgId = _imgIdArray;
    [browser show];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
